package com.chen.basic.service.impl;

import com.chen.basic.mapper.BaseMapper;
import com.chen.basic.query.PageQuery;
import com.chen.basic.service.IBaseService;
import com.chen.basic.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

/**
 * 抽取实现类的Service中的公共类
 */
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {

    @Autowired
    private BaseMapper<T> baseMapper;

    @Transactional
    @Override
    public void add(T t) {
        baseMapper.insert(t);
    }

    @Transactional
    @Override
    public void deleteById(Serializable id) {
        baseMapper.deleteById(id);
    }

    @Transactional
    @Override
    public void update(T t) {
        baseMapper.update(t);
    }

    @Override
    public T getById(Serializable id) {
        return baseMapper.selectById(id);
    }

    @Override
    public List<T> getAll() {
        return baseMapper.selectAll();
    }

    @Transactional
    @Override
    public void batchDelete(List<Long> ids) {
        baseMapper.batchDelete(ids);
    }

    /**
     * 分页 : 总条数,当前页数据
     * @param query
     * @return
     */
    @Override
    public PageList<T> listPage(PageQuery query) {
        long total = baseMapper.queryTotal(query);
        if(total > 0){
            List<T> rows = baseMapper.queryAllPage(query);
            return new PageList<>(total, rows);
        }
        return new PageList<>();
    }
}
